<?php
session_start();
include_once 'functions/function.php';
include_once 'functions/newTopic.class.php';
include 'config/dataSource.php';
include 'addFile.class.php';
$Db = Db();


if (!isset($_SESSION['id'])) {

     
    session_start();
    session_unset();
    session_destroy();

    header('Location: index.php');
}


if (isset($_POST['topic']) AND isset($_POST['comment']) AND isset($_FILES['uploadFile'])) {
    
    $file;
    $fileStatus = false;
    if (isset($_FILES['uploadFile'])) {
        
        $file = new File($_FILES['uploadFile']);
        if ($_FILES['uploadFile']['error'] == 0) {
            $fileStatus = true;
        }
    }

    $newTopic = new newTopic($_POST['topic'], $_POST['comment'], $_POST['forumId1']);

    $error = $newTopic->check();

    if ($error == "ok") {

        if ($newTopic->add()) {
            if ($fileStatus) {

                $fileUser = $_SESSION['id'];
                $forumID;
                $topicID;
                $replyID;
                $forumIDQuery = mysql_query("SELECT tid,forumID FROM topic WHERE topicTitle='{$_POST['topic']}'");

                if (mysql_num_rows($forumIDQuery) > 0) {
                    $row = mysql_fetch_assoc($forumIDQuery);
                    $forumID = $row['forumID'];

                    $topicID = $row['tid'];
                }
                $replyIDQuery = mysql_query("SELECT rid FROM reply WHERE topicID='{$topicID}'");
                if (mysql_num_rows($replyIDQuery) > 0) {
                    while ($row = mysql_fetch_assoc($replyIDQuery)) {
                        $replyID = $row['rid'];
                    }
                }

                
                $file->saveWithReply($fileUser, $forumID, $replyID);
            }

            $query = $Db->prepare("SELECT *FROM forum where fid = :forumId"); #added the AND

            $query->execute(array('forumId' => $_GET['forumId']));

            $response = $query->fetch();
            $forumTit = $response['forumTitle'];
            header("Location: home.php?forumTitle=$forumTit");

            $error = "Success";
            $display = $error;
        } else {

            $error = "Sorry, There was a problem adding your Topic";
            $display = $error;
        }
    } else {

        $display = $error;
    }
}
?>





<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/newTopic.css">
    </head >


    <body style="background: url(images/background.jpg);">


        <div id="wrapper">

            <div id="head">

                <table id="myTable" border=0>
                    <tr>
                        <td class="leftCell" ><span>Hello!! <span class="newAccount"><?php echo $_SESSION['username']; ?></a></span></td>
                        <td class="middleCell" ><a class="newAccount" href="home.php">Home</a></td>
                        <td class="rightCell" ><span><a class="newAccount" href="logOut.php">Sign out</a></span></td>
                    </tr>
                </table>

            </div>

            <div id="container">


                <div id="optionSection">

                    <p class="newForum"><a class="noDecoration3" href="newForum.php">Create a New Forum</a></p>

                </div>



                <div id="myTable2">

                    <div id="postSection">


                        <fieldset>
                            <legend class="legend">Add a New Topic</legend>


                            <form enctype="multipart/form-data" method="post" action="newTopic.php?forumId=<?php echo $_GET['forumId']; ?>">



                                <p class="subject"><input  type="text" name="topic" placeholder="Topic Title" size="40"/></p>


                                <p class="comment"><textarea class="comment" name="comment" placeholder="Comment"></textarea></p>
                                <input type="file" name="uploadFile">
                                <br/>
                                
                                <p><input  type="hidden" name="forumId1" value="<?php echo $_GET['forumId']; ?>"/></p>

                                <p class="submitAlign"><input id="submit" type="submit" value="Create Topic"/></p>

                            </form>

                        </fieldset>

                        <p id="error">

                            <?php
                            if (isset($display)) {

                                echo $display;
                            }
                            ?>


                        </p>

                    </div>

                </div>



            </div>

        </div>



    </body>
</html>

